package com.z.utils.property;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URLDecoder;
import java.util.Properties;

/**
 * 通用配置文件
 * 
 * @author wulei
 *
 */
public class CommonPropertyUtil {
	//public static PropertyFile property = null;
	//public static Properties datas = null;
	public static String roleProperty = "roleSet";
	public static String noticeProperty = "notice";

	static {
		//property = new PropertyFile("kqSql");
		BufferedReader br = null;
		Properties datas = new Properties();
		try {
			String fileDir = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("").getPath(),"UTF-8");
			//System.out.println(fileDir);
			br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileDir+"common.properties")), "UTF-8"));
			datas.load(br);
			//System.out.println(datas.getProperty("finderUserInPost"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 根据key在默认配置文件中查找值
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		return getValue("common",key);
	}
	
	/**
	 * 根据key在指定配置文件名称查询值
	 * @param name
	 * @param key
	 * @return
	 */
	public static String getValue(String name, String key) {
		String rtn = "";
		try {
			if (StringUtils.isNotBlank(key)) {
				BufferedReader br = null;
				Properties datas = new Properties();
				String fileDir = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("").getPath(),"UTF-8");
//				System.out.println(fileDir);
				br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileDir + name + ".properties")), "UTF-8"));
				datas.load(br);
				rtn = datas.getProperty(key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}

}
